package com.szc.demo01.exception;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class GlobalException {

    @ExceptionHandler(value = {Exception.class})
    public String allExceptionHandler(HttpServletRequest request, Exception exception){


        Map<String, Object> map = new HashMap<>();
        //传入我们自己的错误状态码 4xx 5xx，否则就不会进入定制错误页面的解析流程
        request.setAttribute("javax.servlet.error.status_code", 500);
        if (exception instanceof NotBossException) {
            //不是boss操作数据
            map.put("message", exception.getMessage());
        }

        //转发到/error
        return "forward:/error";
    }
}
